package cn.com.sbell.vss.auth.db;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import cn.com.sbell.vss.ICommander;
import cn.com.sbell.vss.ILogCommander;
import cn.com.sbell.vss.Response;
import cn.com.sbell.vss.Util;
import cn.com.sbell.vss.VissSession;
import cn.com.sbell.vss.VssConstants;
import cn.com.sbell.vss.VssException;
import cn.com.sbell.vss.ems.dao.IUserDao;
import cn.com.sbell.vss.ems.dao.IUserRoleDao;
import cn.com.sbell.vss.ems.entity.User;
import cn.com.sbell.vss.ems.entity.UserRole;
import cn.com.sbell.vss.util.RandomGUID;
import cn.com.sbell.vss.util.VssValidator;

public class InterDomainAuthByDatabase implements ICommander, ILogCommander {
	private static final Log log = LogFactory
	.getLog(AuthenticatedByDatabase.class);

private IUserDao userDao;

private IUserRoleDao userRoleDao;

private String localIp = "127.0.0.1";

private int nasType = -1;

	public String getLocalIp() {
	return localIp;
}
public void setLocalIp(String localIp) {
	this.localIp = localIp;
}
public int getNasType() {
	return nasType;
}
public void setNasType(int nasType) {
	this.nasType = nasType;
}
	public Response execute(HttpServletRequest request) {
		// TODO Auto-generated method stub
		Response response = Response.createResponse(request);
		String username = request.getParameter("__username");
		String password = request.getParameter("__password");
		if (Util.strIsNull(username)) {
			username = request.getParameter("username");
		}
		if (Util.strIsNull(password)) {
			password = request.getParameter("password");
		}
		String host = Util.getTrimParameter(request, "host");
		String _alarmPort = Util.getTrimParameter(request, "alarmPort");
		String _vapPath = Util.getTrimParameter(request, "vapPath");
		if (host.length() == 0) {// get the client host
			host = request.getRemoteAddr();
			if (host == null || host.length() == 0) {
				host = request.getRemoteHost();
			}
		}

		VssValidator.validateMaxLength("field.user.username", username, true,
				32);
		VssValidator.validateMaxLength("field.user.password", password, false,
				32);
		VssValidator.validateNumber("field.alarmPort", _alarmPort, false,
				VssConstants.DATA_INTEGER);
		VssValidator.validateRequired("field.host", host);

		// prepare query parameters
		Map paramMap = new HashMap();

		paramMap.put("username", username);

		List users = userDao.find(paramMap);
		if (users == null || users.size() != 1) {
			throw new VssException(VssConstants.CODE_NEXTDOMAIN_AUTHENTICATE_FIAL);
		}

		User user = (User) users.get(0);
		if (user.isDisabled()) {
			throw new VssException(VssConstants.CODE_USER_DISABLED);
		}
		if (user.getPassword() != null && !user.getPassword().equals(password)) {
			throw new VssException(VssConstants.CODE_NEXTDOMAIN_AUTHENTICATE_FIAL);
		}
		if (user.getUserType() == 3// domain user
				&& request.getParameter("__domainId") == null) {
			throw new VssException(
					VssConstants.CODE_CANNOT_USE_THIS_USERNAME_LOGIN);
		}
			// set roleId
		Map param = new HashMap();
		param.put("userId", user.getId());
		List list = userRoleDao.find(param);

		Map areaRoleMap = null;
		String curDid = null;
		for (Iterator iter = list.iterator(); iter.hasNext();) {
			UserRole ur = (UserRole) iter.next();
			if (!ur.getDomainId().equals(curDid)) {
				curDid = ur.getDomainId();
				areaRoleMap = new HashMap();
				
			}
			areaRoleMap.put(ur.getAreaId(), ur.getRoleId());
		}
		log.debug("InterDomainAuthByDatabase succeed!");
		
		
		Map parameters = new java.util.LinkedHashMap();
		parameters.put("userId", user.getId());
		parameters.put("username", username);
		parameters.put("nextDomainUsername","root" );
		parameters.put("nextDomainPassword",Util.MD5( "12345678"));
		// set parameters xml string, then return
		response.setParameters(parameters);
		return response;

	}
	public static Log getLog() {
		return log;
	}
	public IUserDao getUserDao() {
		return userDao;
	}
	public void setUserDao(IUserDao userDao) {
		this.userDao = userDao;
	}
	public IUserRoleDao getUserRoleDao() {
		return userRoleDao;
	}
	public void setUserRoleDao(IUserRoleDao userRoleDao) {
		this.userRoleDao = userRoleDao;
	}

}
